<?php

namespace App\Admin\Controllers;

use App\Models\ShopMenu;
use App\Models\Shop;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ShopMenuController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');
            $content->body($this->form());
        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ShopMenu::class, function (Grid $grid) {
            $grid->disableCreateButton();//禁用新增
            $grid->filter(function($filter){
                $filter->disableIdFilter();
                $filter->equal('shop_id', '店铺id');
                $filter->equal('shop.shop_name', '店铺名');
                $filter->equal('menu_name', '菜单名');
            });
            if(request('shop_id')!=''){
                $grid->model()->where('shop_id',request('shop_id'));
            }
            $grid->id('ID')->sortable();
            $grid->column('shop_id','店铺id');
            $grid->column('shop.shop_name','店铺名');
            $grid->column('menu_name','菜单名');
            $grid->column('menu_sort','序号');
            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ShopMenu::class, function (Form $form) {

            /* $form->saving(function (Form $form) {
                 if($form->shop['shop_name']) {
                     $shop = Shop::where('shop_name', $form->shop['shop_name'])->first();
                     if(!$shop){
                         echo '<script>alert("该店铺不存在！");history.back();</script>';die;
                     }
                 }
                 if(request('shop_id')) {
                     try {
                         ShopMenu::create(['shop_id' => request('shop_id'), 'menu_name' => $form->menu_name, 'menu_sort' => $form->menu_sort]);
                         echo '<script>alert("保存成功");history.back();</script>';die;
                     }catch (\Exception $e) {
                         echo '<script>alert("保存失败");history.back();</script>';die;
                     }
                 }else{
                     try {
                         ShopMenu::create(['shop_id' => $shop->id, 'menu_name' => $form->menu_name, 'menu_sort' => $form->menu_sort]);
                     }catch (\Exception $e){
                         echo "<script> alert('保存失败！');history.back(); </script>";die;
                     }
                     echo '<script>alert("保存成功");history.back();</script>';die;
                 }
             });*/
            $form->saved(function (Form $form) {
                echo '<script>alert("保存成功");history.back();</script>';die;
            });
            $form->display('id', 'ID');
            if(request('shop_id')) {
                $shop_name = Shop::where('id', request('shop_id'))->first()->shop_name;
                $form->text('shop_id', '店铺id')->value(request('shop_id'));
                $form->display('shop.shop_name', '店铺名')->value($shop_name);
            }else{
                $form->display('shop.shop_name', '店铺名');
            }
            $form->text('menu_name', '菜单名')->rules('required');
            $form->text('menu_sort', '序号');
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }



}
